﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Neoseeker.NAMFox.Tests.Configuration;
using System.Configuration;
using System.IO;

namespace Neoseeker.NAMFox.Tests
{
    public static class Url
    {
        private static TargetElement targetElement = ((NeoseekerSection)ConfigurationManager.GetSection("neoseeker")).Target;

        public static Uri RootUri
        {
            get { return new Uri(Resolve("http://beta.neoseeker.com/devel/{0}/neoseeker/public_html/", "http://www.neoseeker.com")); }
        }

        /// <summary>
        /// Resolves a URL to the Neoseeker API, given the specified URI path.
        /// </summary>
        /// <param name="path">The path of the URI, to be combined with the root
        /// for the API.</param>
        public static string Api(string path)
        {
            return Resolve("http://beta.neoseeker.com/devel/{0}/neoseeker/api/", "http://api.neoseeker.com/", path);
        }

        /// <summary>
        /// Resolves a URL to the blog pages, given the specified URI path.
        /// </summary>
        /// <param name="path">The path of the URI, to be combined with the root
        /// for blogs.</param>
        public static string Blog(string path)
        {
            return Resolve("http://beta.neoseeker.com/devel/{0}/neoseeker/blogs/", "http://blogs.neoseeker.com/", path);
        }

        /// <summary>
        /// Resolves a URL to the forum pages, given the specified URI path.
        /// </summary>
        /// <param name="path">The path of the URI, to be combined with the root
        /// for forums.</param>
        public static string Forum(string path)
        {
            return Resolve("http://beta.neoseeker.com/devel/{0}/neoforums/public_html/", "http://www.neoseeker.com/forums/", path);
        }

        /// <summary>
        /// Resolves a URL to the member pages, given the specified URI path.
        /// </summary>
        /// <param name="path">The path of the URI, to be combined with the root
        /// for members.</param>
        public static string Member(string path)
        {
            return Resolve("http://beta.neoseeker.com/devel/{0}/neoseeker/public_html/members/", "http://www.neoseeker.com/members/", path);
        }

        private static string Resolve(string devRoot, string liveRoot)
        {
            return Resolve(devRoot, liveRoot, String.Empty);
        }

        private static string Resolve(string devRoot, string liveRoot, string path)
        {
            if (targetElement.Environment == TargetEnvironment.Dev)
            {
                return String.Format(devRoot, targetElement.Developer) + RemoveLeadingForwardSlash(path);
            }

            return liveRoot + RemoveLeadingForwardSlash(path);
        }

        private static string RemoveLeadingForwardSlash(string input)
        {
            if (input.StartsWith("/"))
            {
                return input.Substring(1);
            }

            return input;
        }
    }
}
